Sensor Fusion for Kinetis MCUs (ISSDK/KSDK version)
main_baremetal.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016, NXP Semiconductor
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * o Redistributions of source code must retain the above copyright notice, this list
9  * of conditions and the following disclaimer.
10  *
11  * o Redistributions in binary form must reproduce the above copyright notice, this
12  * list of conditions and the following disclaimer in the documentation and/or
13  * other materials provided with the distribution.
14  *
15  * o Neither the name of NXP Semiconductor, nor the names of its
16  * contributors may be used to endorse or promote products derived from this
17  * software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 /*! \file main_baremetal.c
31  \brief Bare metal implementation of sensor fusion on FRDM-K64F.
32 
33  This file shows the recommended way to incorporate sensor fusion capabilities
34  into a bare metal (no RTOS) project.
35 */
36 
37 // KSDK and ISSDK Headers
38 #include "fsl_debug_console.h" // KSDK header file for the debug interface
39 #include "board.h" // KSDK header file to define board configuration
40 #include "pin_mux.h" // KSDK header file for pin mux initialization functions
41 #include "clock_config.h" // KSDK header file for clock configuration
42 #include "fsl_port.h" // KSDK header file for Port I/O control
43 #include "fsl_i2c.h" // KSDK header file for I2C interfaces
44 #include "fsl_pit.h" // KSDK header feile for Periodic Interval Timer
45 #include "Driver_I2C_SDK2.h" // ISSDK CMSIS I2C Driver
46 #include "fxas21002.h" // Register and bit-field definitions
47 #include "mpl3115.h" // Register and bit-field definitions
48 #include "fxos8700.h" // Register and bit-field definitions
49 #include "fsl_smc.h"
50 
51 // Sensor Fusion Headers
52 #include "sensor_fusion.h" // top level magCal and sensor fusion interfaces
53 #include "control.h" // Command/Streaming interface - application specific
54 #include "status.h" // Status indicator interface - application specific
55 #include "drivers.h" // NXP sensor drivers OR customer-supplied drivers
56 #include "driver_pit.h" // Project-specific - PIT is used to control main() timing loop
57 
58 // Global data structures
59 SensorFusionGlobals sfg; ///< This is the primary sensor fusion data structure
60 ControlSubsystem controlSubsystem; ///< used for serial communications
61 StatusSubsystem statusSubsystem; ///< provides visual (usually LED) status indicator
62 PhysicalSensor sensors[3]; ///< This implementation uses up to 3 sensors
63 
64 /// This is a bare-metal implementation of the NXP sensor fusion demo build.
65 int main(void)
66 {
67  ARM_DRIVER_I2C* I2Cdrv = &I2C_S_DRIVER_BLOCKING; // defined in the <shield>.h file
68  uint16_t i=0; // general counter variable
69  BOARD_InitPins(); // defined in pin_mux.c, initializes pkg pins
70  BOARD_BootClockRUN(); // defined in clock_config.c, initializes clocks
71  BOARD_InitDebugConsole(); // defined in board.c, initializes the OpenSDA port
72  I2Cdrv->Initialize(NULL); // Initialize the KSDK driver for the I2C port
73 
74  I2Cdrv->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST); // Configure the I2C bus speed
75 
76  initializeControlPort(&controlSubsystem); // configure pins and ports for the control sub-system
77  initializeStatusSubsystem(&statusSubsystem); // configure pins and ports for the status sub-system
78  initSensorFusionGlobals(&sfg, &statusSubsystem, &controlSubsystem); // Initialize sensor fusion structures
79  // "install" the sensors we will be using
80 #if F_USING_ACCEL || F_USING_MAG
81  sfg.installSensor(&sfg, &sensors[0], FXOS8700_I2C_ADDR, 1, (void*) I2Cdrv, FXOS8700_Init, FXOS8700_Read);
82 #endif
83 #if F_USING_GYRO
84  sfg.installSensor(&sfg, &sensors[1], FXAS21002_I2C_ADDR, 1, (void*) I2Cdrv, FXAS21002_Init, FXAS21002_Read);
85 #endif
86 #if F_USING_PRESSURE
87  sfg.installSensor(&sfg, &sensors[2], MPL3115_I2C_ADDR, 1, (void*) I2Cdrv, MPL3115_Init, MPL3115_Read);
88 #endif
89  sfg.initializeFusionEngine(&sfg); // This will initialize sensors and magnetic calibration
90 
91  pit_init(1000000/FUSION_HZ); // pitIsrFlag will be set true at FUSION_HZ periodic intervals
92 
93  sfg.setStatus(&sfg, NORMAL); // If we got this far, let's set status state to NORMAL
94  while (true)
95  {
96  if (true == pitIsrFlag) { // Check whether occur interupt and toggle LED
97  sfg.readSensors(&sfg, 1); // Reads sensors, applies HAL and does averaging (if applicable)
98  sfg.conditionSensorReadings(&sfg); // magCal is run as part of this
99  sfg.runFusion(&sfg); // Run the actual fusion algorithms
100  sfg.applyPerturbation(&sfg); // apply debug perturbation (testing only)
101  sfg.loopcounter++; // The loop counter is used to "serialize" mag cal operations
102  i=i+1;
103  if (i>=4) { // Some status codes include a "blink" feature. This loop
104  i=0; // should cycle at least four times for that to operate correctly.
105  sfg.updateStatus(&sfg); // This is where pending status updates are made visible
106  }
107 
108  sfg.queueStatus(&sfg, NORMAL); // assume NORMAL status for next pass through the loop
109  sfg.pControlSubsystem->stream(&sfg, sUARTOutputBuffer); // Send stream data to the Sensor Fusion Toolbox
110  pitIsrFlag = false; // Reset the flag for the next cycle
111  }
112  }
113 }
114 /// \endcode
void initSensorFusionGlobals(SensorFusionGlobals *sfg, StatusSubsystem *pStatusSubsystem, ControlSubsystem *pControlSubsystem)
utility function to insert default values in the top level structure
Definition: sensor_fusion.c:68
int32_t loopcounter
counter incrementing each iteration of sensor fusion (typically 25Hz)
conditionSensorReadings_t * conditionSensorReadings
preprocessing step for sensor fusion
int8_t FXOS8700_Read(PhysicalSensor *sensor, SensorFusionGlobals *sfg)
void initializeStatusSubsystem(StatusSubsystem *pStatus)
initializeStatusSubsystem() should be called once at startup to initialize the data structure and to ...
Definition: status.c:185
PhysicalSensor sensors[3]
This implementation uses up to 3 sensors.
The top level fusion structure.
streamData_t * stream
function to create packets for serial stream
Definition: control.h:73
installSensor_t * installSensor
function for installing a new sensor into t
StatusSubsystem() provides an object-like interface for communicating status to the user...
Definition: status.h:44
ControlSubsystem controlSubsystem
used for serial communications
void pit_init(uint32_t microseconds)
Definition: driver_pit.c:62
readSensors_t * readSensors
read all physical sensors
struct ControlSubsystem * pControlSubsystem
initializeFusionEngine_t * initializeFusionEngine
set sensor fusion structures to initial values
int8_t MPL3115_Init(PhysicalSensor *sensor, SensorFusionGlobals *sfg)
updateStatus_t * updateStatus
status=next status
int main(void)
This is a bare-metal implementation of the NXP sensor fusion demo build.
int8_t FXOS8700_Init(PhysicalSensor *sensor, SensorFusionGlobals *sfg)
volatile bool pitIsrFlag
Definition: driver_pit.c:53
int8_t FXAS21002_Read(PhysicalSensor *sensor, SensorFusionGlobals *sfg)
The sensor_fusion.h file implements the top level programming interface.
int8_t MPL3115_Read(PhysicalSensor *sensor, SensorFusionGlobals *sfg)
Provides function prototypes for driver level interfaces.
he ControlSubsystem encapsulates command and data streaming functions.
Definition: control.h:64
uint8_t sUARTOutputBuffer[256]
main output buffer defined in control.c
Definition: control.c:59
int8_t initializeControlPort(ControlSubsystem *pComm)
Initialize the control subsystem and all related hardware.
Definition: control.c:182
runFusion_t * runFusion
run the fusion routines
Provides a simple abstraction for a periodic interval timer.
ARM_DRIVER_I2C * I2Cdrv
KSDK handle for the I2C port defined in Driver_I2C_KSDK2.c.
Application-specific status subsystem.
SensorFusionGlobals sfg
This is the primary sensor fusion data structure.
An instance of PhysicalSensor structure type should be allocated for each physical sensors (combo dev...
setStatus_t * queueStatus
queue status change for next regular interval
#define FUSION_HZ
(int) actual rate of fusion algorithm execution and sensor FIFO reads
Defines control sub-system.
StatusSubsystem statusSubsystem
provides visual (usually LED) status indicator
setStatus_t * setStatus
change status indicator immediately
applyPerturbation_t * applyPerturbation
apply step function for testing purposes
int8_t FXAS21002_Init(PhysicalSensor *sensor, SensorFusionGlobals *sfg)
Operation is Nominal.